package com.zkdcloud.aspectInterceptor.validatie;

import com.xdja.interceptor.exception.InvokeException;
import com.xdja.interceptor.util.ValidateUtils;
import com.zkdcloud.domain.Admin;
import com.zkdcloud.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 登录校验
 *
 * @author zk123
 * @version 2018/4/30
 */
@Component
public class LoginControllerValidate {
    /**
     * adminService
     */
    @Autowired
    private AdminService adminService;

    /**
     * 校验admin 账号密码不能为null validate
     *
     * @param admin admin
     * @throws InvokeException ex
     */
    public void validateAdminUAP(Admin admin) throws InvokeException {
        ValidateUtils.assertCondition(!StringUtils.isEmpty(admin.getUserName()) && !StringUtils.isEmpty(admin.getPassword()),
                "用户名或密码不能为空");
    }

    /**
     * 校验admin 用户名是否重复
     *
     * @param admin admin
     */
    public void checkSameUserName(Admin admin) throws InvokeException {
        String username = admin.getUserName();

        List<Admin> adminList = adminService.getByUserName(username);
        if(adminList != null && !adminList.isEmpty()){
            throw new InvokeException("用户名不能相同");
        }
    }
}
